<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reference extends CI_Controller {

	private $tab = 'user';
	private $sub = '';
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}

	private function invite_data($limit = false){
        $this->load->model('Borrow_model');

		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'   => trim($this->input->get('username')),
			'invite_username'   => trim($this->input->get('invite_username')),
            'begin_time'   => trim($this->input->get('begin_time')),
            'end_time'   => trim($this->input->get('end_time')),
		);

		$this->load->model('User_model', 'user');

		if($limit && $limit='all'){
			$data = array('limit'=>'all');
		}else{
			$data = array('per_page'=>$per_page,'page_size'=>$page_size);
		}
		$vdata = $this->user->invite_list($parms,$data);
		$tmplist = array();
		foreach($vdata['user_list'] as $uo){
            //获取上级和顶级推荐人的奖励金额
            $referee_reward = $this->Borrow_model->referee_reward($uo['user_id']);
            $uo['yiji_reward'] = $referee_reward['yiji_reward_money'];
            $uo['dingji_reward'] = $referee_reward['dingji_reward_money'];

			$tmpuser = $this->user->get_user_by_username($uo['invite_username']);
			$tusername = $this->user->get_username_by_id($tmpuser['invite_userid']);
			$uo['dj_username'] = $tusername['username'];
			$tmplist[] = $uo;
		}
		$vdata['user_list'] = $tmplist;
		$vdata['parms'] = $parms;
		return $vdata;
	}

	function index(){

		$vdata = $this->invite_data();
		$parms = $vdata['parms'];

		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/reference?username='.$parms['username'].'&invite_username='.$parms['invite_username'];
		$config['total_rows'] = $vdata['user_count'];
		$config['per_page'] = 10;
		$this->pagination->initialize($config);
		$vdata['title'] = '推荐列表';



		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'invite';

		$this->load->view('/ychadmin/invite_list', $vdata);
	}


	function invite_excel(){
		//$vdata = $this->invite_data('all');
                $vdata = $this->invite_data();

		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory');

		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("推荐信息")->setDescription(COMPANY_NAME);

		$objPHPExcel->setActiveSheetIndex(0);
		//$titles = array("编号", "推荐人", "投资人", "投资总金额", "投资总笔数");
                $titles = array("ID", "投资人", "上级推荐人", "顶级推荐人", "投资总金额", "投资总笔数");

		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}

		$user_list = $vdata['user_list'];


		for($i = 0; $i < count($user_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $user_list[$i]['user_id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $user_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $user_list[$i]['invite_username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $user_list[$i]['dj_username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $user_list[$i]['money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $user_list[$i]['count']);
		}

		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="推荐信息.xls"');
		header('Cache-Control: max-age=0');
		$objWriter->save('php://output');
		exit;
	}

}